package dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.runner.Describable;

import dao.DepartmentDao;
import po.emp.Department;

public class DepartmentDaoImpl implements DepartmentDao{
	private List<Department> departments;
	

	public DepartmentDaoImpl() {
		departments = new ArrayList<>();
	}


	@Override
	public boolean insert(Department t) {
		if(departments.contains(t)) {
			return false;
		}
		return departments.add(t);
	}


	@Override
	public boolean update(Department t) {
		int index = departments.indexOf(t);
		if(index == -1) {
			return false;
		}else {
			departments.set(index, t);
			return true;
		}
	}


	@Override
	public boolean delete(int id) {
		Iterator<Department> it = departments.iterator();
		while(it.hasNext()) {
			Department d = it.next();
			if(d.getDepartId() == id) {
				it.remove();
				return true;
			}
		}
		return false;
	}


	@Override
	public Department selectById(int id) {
		for(Department d : departments) {
			if(d.getDepartId() == id) {
				return d;
			}
		}
		return null;
	}


	@Override
	public List<Department> selectAll() {
		List<Department> copy = new ArrayList<>(departments);
		return copy;
	}
	
	


	

	


}
